﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    public partial class FSearchAuction : Form
    {
        private EDM_AuctionContainer context;
        FormBase frmSuper;
        Auction auctionFound = null;
        Validations valid = new Validations();
        public FSearchAuction(FormBase frmSuper, EDM_AuctionContainer context)
        {
            InitializeComponent();
            this.frmSuper = frmSuper;
            this.context=context;
        }

        private void rdbSearchById_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchById.Checked)
            {
                grb_ID.Enabled = true;
                b_SearchAuction.Enabled = true;
            }
            else
            {
                grb_ID.Enabled = false;
            }
        }

        private void rdbSearchByName_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchByName.Checked)
            {
                grb_Name.Enabled = true;
                b_SearchAuction.Enabled = true;
            }
            else
            {
                grb_Name.Enabled = false;
            }
        }

        private void rdbSearchByDate_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchByDate.Checked)
            {
                grb_Date.Enabled = true;
                b_SearchAuction.Enabled = true;
            }
            else
            {
                grb_Date.Enabled = false;
            }
        }

        private void b_SearchAuction_Click(object sender, EventArgs e)
        {
            lblAdvNotFound.Visible = false;
            auctionFound = null;
            if (rdbSearchById.Checked)
            {
                int id=valid.GetInt(t_Id);
                try
                {
                    auctionFound = context.AuctionSet.FirstOrDefault(c => c.idAuction == id);
                }
                catch (System.Exception)
                {
                    auctionFound = null;
                }
            }
            else if (rdbSearchByName.Checked)
            {
                try
                {
                    auctionFound = context.AuctionSet.FirstOrDefault(c => c.nameAuction == t_Name.Text);
                }
                catch (System.Exception)
                {
                    auctionFound = null;
                }
            }
            else if (rdbSearchByName.Checked)
            {
                try
                {
                    auctionFound = context.AuctionSet.FirstOrDefault(c => c.dateAuction == dt_date.Value);
                }
                catch (System.Exception)
                {
                    auctionFound = null;
                }
                
            }
            if (auctionFound !=null)
            {
                ((FormBase)this.frmSuper).TransferAuctionFound(auctionFound); ;
            }
            else
            {
                lblAdvNotFound.Visible = true;
            }
        }

        private void FSearchAuction_Load(object sender, EventArgs e)
        {

        }

    }
}
